using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DreamBuilder.DictionaryAccess;



namespace DreamBuilder.UI.UserControls
{
    public class BoolTemplateColumn : IChildItem
    {

        private ChildTableItemInfo childTableItemInfo;
        DDLBool ddlBool = null;
        private UserControl userControl;
        public event ItemSelectedEventHandler ItemSelected;
        public BoolTemplateColumn()
        {

        }

        public BoolTemplateColumn(ChildArgs childArgs)
        {
            userControl = childArgs.Container;
            this.childTableItemInfo = childArgs.CChildTableItemInfo;
        }
        public void SetControlID()
        {
            ddlBool.ID = "cbo_" + this.childTableItemInfo.FieldCode;
        }
        public void SetControlAttributes()
        {
            ddlBool.AutoPostBack = childTableItemInfo.AutoPostBack;
            if (childTableItemInfo.AutoPostBack == true)
            {
                ddlBool.RefSelected += new RefSelectBoolHandler(ddlBool_RefSelected);
            }
        }

        void ddlBool_RefSelected(object sender, EventArgs e)
        {
            DDLBool ddlBool = sender as DDLBool;
            GridViewRow gvRow = ddlBool.NamingContainer as GridViewRow;
            GridView gvChild = gvRow.NamingContainer as GridView;
            UCChild ucChild = gvChild.Parent as UCChild;
            DataTable curentData = ucChild.OutPutDataTable;
            int index = int.Parse(ddlBool.Attributes["RowIndex"]);
            object key = gvChild.DataKeys[index][0];
            DataRow row = curentData.Rows.Find(key);
            row[ddlBool.Attributes["FieldCode"].ToString()] = ddlBool.Value;
            ucChild.Refresh(ref curentData);
            if (ddlBool != null && gvRow != null)
            {
                ChildItemEventArgs childItemEventArgs = new ChildItemEventArgs();
                childItemEventArgs.CurrentEventItem = gvRow;
                if (ItemSelected != null)
                {
                    ItemSelected(ddlBool, childItemEventArgs);
                }
            }
        }

        public void SetControlValue(GridViewRow gvRow, string controlID, FieldDicInfo fieldDicInfo, DataRow dr)
        {
            DDLBool currDDLBool = gvRow.FindControl(controlID) as DDLBool;
            if (currDDLBool != null && dr[fieldDicInfo.FieldCode] != System.DBNull.Value)
            {
                currDDLBool.Attributes.Add("RowIndex", gvRow.RowIndex.ToString());
                currDDLBool.Attributes.Add("FieldCode", fieldDicInfo.FieldCode);
                currDDLBool.Value = Convert.ToBoolean(Convert.ToInt32(dr[fieldDicInfo.FieldCode]));
            }
        }

        public void GetControlValue(GridViewRow gvRow, DataRow dr, string controlID, ChildTableItemInfo fieldDicInfo)
        {
            DDLBool currDDLBool = gvRow.FindControl(controlID) as DDLBool;
            if (currDDLBool != null)
            {
                dr[fieldDicInfo.FieldCode] = currDDLBool.Value == true ? 1 : 0;
            }
        }

        public void InstantiateIn(Control container)
        {
            ddlBool = (DDLBool)userControl.LoadControl("DDLBool.ascx");
            SetControlID();
            SetControlAttributes();
            container.Controls.Add(ddlBool);
        }
    }
}

